﻿#include "unrealircd.h"

/*
此模块存放数据库查询返回对象的处理函数

*/

int db_PQresult_To_D_User(PGresult *result, DB_Rsult_Data *result_data);


/*
@todo:将数据库查询结果映射为结构体对象，对于不同的数据库实体都要有此函数
@params:result为数据库查询结果，result_data为用来返回信息的
*/
int db_PQresult_To_D_User(PGresult *result, DB_Rsult_Data *result_data) {
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_User *d_users = malloc(sizeof(D_User)*ntuples);
	memset(d_users,0,sizeof(D_User)*ntuples);
	int id_col = PQfnumber(result, "id");
	if (id_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, id_col)) {
				char *value = PQgetvalue(result, row, id_col);
				((d_users + row)->id) = malloc(sizeof(int));
				*((d_users + row)->id) = atoi(value);
			}
			else {
				(d_users + row)->id = NULL;
			}
		}
	}
	int git_id_col = PQfnumber(result, "git_id");
	if (git_id_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, git_id_col)) {
				char *value = PQgetvalue(result, row, git_id_col);
				((d_users + row)->git_id) = malloc(sizeof(int));
				*((d_users + row)->git_id) = atoi(value);
			}
			else {
				(d_users + row)->git_id = NULL;
			}
		}
	}
	int user_name_col = PQfnumber(result, "user_name");
	if (user_name_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, user_name_col)) {
				char *value = PQgetvalue(result, row, user_name_col);
				safe_strdup((d_users + row)->user_name,value);
			}
			else {
				(d_users + row)->user_name = NULL;
			}
		}
	}
	int user_password_col = PQfnumber(result, "user_password");
	if (user_password_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, user_password_col)) {
				char *value = PQgetvalue(result, row, user_password_col);
				safe_strdup((d_users + row)->user_password,value);
			}
			else {
				(d_users + row)->user_password = NULL;
			}
		}
	}
	int tel_col = PQfnumber(result, "tel");
	if (tel_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, tel_col)) {
				char *value = PQgetvalue(result, row, tel_col);
				safe_strdup((d_users + row)->tel,value);
			}
			else {
				(d_users + row)->tel = NULL;
			}
		}
	}
	int sex_col = PQfnumber(result, "sex");
	if (sex_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, sex_col)) {
				char *value = PQgetvalue(result, row, sex_col);
				((d_users + row)->sex) = malloc(sizeof(int));
				*((d_users + row)->sex) = atoi(value);
			}
			else {
				(d_users + row)->sex = NULL;
			}
		}
	}
	int created_at_col = PQfnumber(result, "created_at");
	if (created_at_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			(d_users + row)->created_at = PQresult_row_col_To_D_TimeStamp(result,row,created_at_col);
		}
	}
	int user_nick_col = PQfnumber(result, "user_nick");
	if (user_nick_col != -1) {
		for (int row = 0; row < ntuples; row++) {
			if (!PQgetisnull(result, row, user_nick_col)) {
				char *value = PQgetvalue(result, row, user_nick_col);
				safe_strdup((d_users + row)->user_nick,value);
			}
			else {
				(d_users + row)->user_nick = NULL;
			}
		}
	}
	result_data->data = d_users;
	result_data->datalen = ntuples;
	return 0;
}

/*
@todo:将数据库查询结果映射为结构体对象，对于不同的数据库实体都要有此函数
@params:result为数据库查询结果，result_data为用来返回信息的
*/
int db_PQresult_To_D_configure(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_configure *d_configure = malloc(sizeof(D_configure)*ntuples);
	memset(d_configure,0,sizeof(D_configure)*ntuples);
	int col = PQfnumber(result, "configure_name");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].configure_name=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "configure_value");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].configure_value=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "configure_detail");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].configure_detail=PQresult_row_col_To_char(result,row,col);
		}
	}
	result_data->data = d_configure;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_third_login(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_third_login *d_configure = malloc(sizeof(D_third_login)*ntuples);
	memset(d_configure,0,sizeof(D_third_login)*ntuples);
	int col = PQfnumber(result, "third_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].third_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "open_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].open_id=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "user_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].user_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "nike_name");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].nick_name=PQresult_row_col_To_char(result,row,col);
		}
	}

	result_data->data = d_configure;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_im_sms(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_im_sms *d_configure = malloc(sizeof(D_im_sms)*ntuples);
	memset(d_configure,0,sizeof(D_im_sms)*ntuples);
	int col = PQfnumber(result, "identity_no");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].identity_no=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "sms_type");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].sms_type=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "sms_channel");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].sms_channel=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "used");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].used=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "code");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].code=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "contactvalue");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].contactvalue=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "valid_time");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_configure[row].valid_time=PQresult_row_col_To_D_TimeStamp(result,row,col);
		}
	}
	result_data->data = d_configure;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_im_user_email(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_im_user_email *d_userEamil = malloc(sizeof(D_im_user_email)*ntuples);
	memset(d_userEamil,0,sizeof(D_im_user_email)*ntuples);
	int col = PQfnumber(result, "id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_userEamil[row].id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "email");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_userEamil[row].email=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "permition");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_userEamil[row].permition=PQresult_row_col_To_int(result,row,col);
		}
	}
	result_data->data = d_userEamil;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_im_login_record(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_im_login_record *d_data = malloc(sizeof(D_im_login_record)*ntuples);
	memset(d_data,0,sizeof(D_im_login_record)*ntuples);
	int col = PQfnumber(result, "login_no");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].login_no=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "user_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].user_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "log_time");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].log_time=PQresult_row_col_To_D_TimeStamp(result,row,col);
		}
	}
	col = PQfnumber(result, "login_ip");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].login_ip=PQresult_row_col_To_char(result,row,col);
		}
	}
	col = PQfnumber(result, "login_mac");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].login_mac=PQresult_row_col_To_char(result,row,col);
		}
	}
	result_data->data = d_data;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_im_relation_applicant(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_im_relation_applicant *d_data = malloc(sizeof(D_im_relation_applicant)*ntuples);
	memset(d_data,0,sizeof(D_im_relation_applicant)*ntuples);
	int col = PQfnumber(result, "relation_applicant_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].relation_applicant_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "applicant_type");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].applicant_type=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "applicant_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].applicant_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "respondent_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].respondent_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "project_group_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].project_group_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "applicant_status");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].applicant_status=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "applicant_time");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].applicant_time=PQresult_row_col_To_D_TimeStamp(result,row,col);
		}
	}
	result_data->data = d_data;
	result_data->datalen = ntuples;
	return 0;
}

int db_PQresult_To_D_im_user_relation(PGresult *result, DB_Rsult_Data *result_data){
	int ntuples = PQntuples(result);
	if(ntuples==0){
		result_data->data = NULL;
		result_data->datalen = ntuples;
		return 0;
	}
	D_im_user_relation *d_data = malloc(sizeof(D_im_user_relation)*ntuples);
	memset(d_data,0,sizeof(D_im_user_relation)*ntuples);
	int col = PQfnumber(result, "user_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].user_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "friend_id");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].friend_id=PQresult_row_col_To_int(result,row,col);
		}
	}
	col = PQfnumber(result, "nick_name");
	if (col != -1) {
		for (int row = 0; row < ntuples; row++) {
			d_data[row].nick_name=PQresult_row_col_To_char(result,row,col);
		}
	}
	result_data->data = d_data;
	result_data->datalen = ntuples;
	return 0;
}

D_User * db_createUser(){
	D_User *user=safe_alloc(sizeof(D_User));
	user->created_at=NULL;
	user->git_id=NULL;
	user->id=NULL;
	user->sex=NULL;
	user->tel=NULL;
	user->user_name=NULL;
	user->user_nick=NULL;
	user->user_password=NULL;
	return user;
}

D_im_user_email *db_createUserEmail(){
	D_im_user_email* user_email = safe_alloc(sizeof(D_im_user_email));
	user_email->email = NULL;
	user_email->id = NULL;
	user_email->permition = NULL;
	return user_email;
}

D_configure * db_createConfigure(){
	D_configure *configure=safe_alloc(sizeof(D_configure));
	configure->configure_detail = NULL;
	configure->configure_name = NULL;
	configure->configure_value = NULL;
	return configure;
}

D_im_sms * db_createImSMS(){
	D_im_sms *sms = safe_alloc(sizeof(D_im_sms));
	sms->identity_no = NULL;
	sms->sms_type = NULL;
	sms->sms_channel = NULL;
	sms->used = NULL;
	sms->code = NULL;
	sms->contactvalue = NULL;
	sms->valid_time = NULL;
	return sms;
}

D_third_login * db_createThirdLogin(){
	D_third_login *login = safe_alloc(sizeof(D_third_login));
	login->third_id = NULL;
	login->open_id = NULL;
	login->user_id = NULL;
	login->nick_name = NULL;
	return login;
}

D_im_login_record * db_createLoginRecord(){
	D_im_login_record* data = safe_alloc(sizeof(D_im_login_record));
	data->log_time = NULL;
	data->login_ip = NULL;
	data->login_mac = NULL;
	data->login_no = NULL;
	data->user_id = NULL;
	return data;
}

D_im_relation_applicant * db_createRelationApplicant(){
	D_im_relation_applicant* data = safe_alloc(sizeof(D_im_relation_applicant));
	memset(data,0,sizeof(data));
	// data->relation_applicant_id = NULL;
	// data->applicant_type = NULL;
	// data->applicant_id = NULL;
	// data->respondent_id = NULL;
	// data->project_group_id = NULL;
	// data->applicant_status = NULL;
	// data->applicant_time = NULL;
	return data;
}

D_im_user_relation * db_createUserRelation(){
	D_im_user_relation* data = safe_alloc(sizeof(D_im_user_relation));
	data->user_id = NULL;
	data->friend_id = NULL;
	data->nick_name = NULL;
	return data;
}


void db_D_UserFree(D_User *user, int len){
	if(!user){
		return;
	}
	for(int i=0; i<len; i++){
		safe_free(user[i].id);
		safe_free(user[i].git_id);
		safe_free(user[i].user_name);
		safe_free(user[i].user_password);
		safe_free(user[i].tel);
		safe_free(user[i].sex);
		safe_free(user[i].created_at);
		safe_free(user[i].user_nick);
	}
	safe_free(user);
}

void db_D_ImSMSFree(D_im_sms* sms,int len){
	if(!sms){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(sms[i].identity_no);
		safe_free(sms[i].sms_type);
		safe_free(sms[i].sms_channel);
		safe_free(sms[i].used);
		safe_free(sms[i].code);
		safe_free(sms[i].contactvalue);
		safe_free(sms[i].valid_time);
	}

	safe_free(sms);
}

void db_D_ThirdLoginFree(D_third_login * login,int len){
	if(!login){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(login[i].third_id);
		safe_free(login[i].open_id);
		safe_free(login[i].user_id);
		safe_free(login[i].nick_name);
	}
	safe_free(login);
}

/**
 * 释放数据库实体对象D_configure
 */
void db_D_configure_free(D_configure *d_configure,int len){
	if(len<=0||d_configure==NULL){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(d_configure[i].configure_value);
		safe_free(d_configure[i].configure_name);
		safe_free(d_configure[i].configure_detail);
	}
	safe_free(d_configure);
}

void db_D_UserEmailFree(D_im_user_email * user_email,int len){
	if(!user_email){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(user_email[i].email);
		safe_free(user_email[i].id);
		safe_free(user_email[i].permition);
	}
	safe_free(user_email);

}
void db_D_LoginRecordFree(D_im_login_record* data,int len){
	if(!data){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(data[i].log_time);
		safe_free(data[i].login_ip);
		safe_free(data[i].login_mac);
		safe_free(data[i].login_no);
		safe_free(data[i].user_id);
	}
	safe_free(data);
}

void db_D_imRelationApplicantFree(D_im_relation_applicant* data,int len){
	if(!data){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(data[i].relation_applicant_id);
		safe_free(data[i].applicant_type);
		safe_free(data[i].applicant_id);
		safe_free(data[i].respondent_id);
		safe_free(data[i].project_group_id);
		safe_free(data[i].applicant_status);
		safe_free(data[i].applicant_time);
	}
	safe_free(data);
}

void db_D_imUserRelationFree(D_im_user_relation* data,int len){
	if(!data){
		return;
	}
	for(int i=0;i<len;i++){
		safe_free(data[i].user_id);
		safe_free(data[i].friend_id);
		safe_free(data[i].nick_name);
	}
	safe_free(data);
}
